<?php
/**
 * @author Administrator
 *
 */
 
 App::import('Vendor', 'Dzstyle', array('file' => 'template'.DS.'Dzstyle.php'));
 
class DzstyleView extends View{
	
	var $theme_path = '';
	//var $controller;
	var $layout_flag = false;
	
	function __construct(&$controller) {
		parent::__construct($controller);
		$this->theme =& $controller->theme;
		//$this->controller = $controller;
		
		$this->ext= '.html';
		
		$this->Dzstyle = &new Dzstyle();
		
		//var_dump($this);
		$this->Dzstyle->model_name=$this->name;
		
		$this->Dzstyle->ext=$this->ext;
		
		$this->Dzstyle->force=1;
			
		$this->Dzstyle->objdir = TMP.'cache'.DS.'dzstyle'.DS;
		if($this->theme)
		{
			$this->Dzstyle->theme_path = VIEWS.'themed'.DS.$this->theme.DS;
		}
		else
		{
			$this->Dzstyle->theme_path = VIEWS;
		}
	}

/**
 * Return all possible paths to find view files in order
 *
 * @param string $plugin
 * @return array paths
 * @access private
 */
	function _paths($plugin = null, $cached = true) {
		$paths = parent::_paths($plugin, $cached);

		if (!empty($this->theme)) {
			$count = count($paths);
			for ($i = 0; $i < $count; $i++) {
				$themePaths[] = $paths[$i] . 'themed'. DS . $this->theme . DS;
			}
			$paths = array_merge($themePaths, $paths);
		}

		if (empty($this->__paths)) {
			$this->__paths = $paths;
		}
		return $paths;
	}
/**
 * Overrides the View::_render()
 * Sets variables used in CakePHP to Smarty variables
 *
 * @param string $___viewFn
 * @param string $___data_for_view
 * @param string $___play_safe
 * @param string $loadHelpers
 * @return rendered views
 */
	function _render($___viewFn, $___data_for_view, $___play_safe = true, $loadHelpers = true)
	{
		if(!$this->layout_flag)
		{
			//echo basename($___viewFn);
			$this->Dzstyle->objfile = $this->Dzstyle->objdir.$this->name.'_'.basename($___viewFn).'.tpl.php';
			// 返回模板的路径，{$content_for_layout}被替换成 路径对应的模板的内容
			
			return $___viewFn;
			// 正文内容模板在输出前不处理，当做layout的子模板；一个页面只对应一个正文模板
			// $___viewFn 的值，在layout模板中的值是 {$content_for_layout}的内容，
			// 在最后处理整体框架的layout模板时，才处理编译模板生成模板缓存
		}
		
		if ($this->helpers != false && $loadHelpers === true)
		{
			$loadedHelpers =  array();
			$loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);

			foreach(array_keys($loadedHelpers) as $helper)
			{
				$replace = strtolower(substr($helper, 0, 1));
				$camelBackedHelper = preg_replace('/\\w/', $replace, $helper, 1);

				${$camelBackedHelper} =& $loadedHelpers[$helper];
				if(isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers))
				{
					foreach(${$camelBackedHelper}->helpers as $subHelper)
					{
						${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
					}
				}
				$this->loaded[$camelBackedHelper] = (${$camelBackedHelper});
				// helps对象 引用传入模板处理对象中
				$this->Dzstyle->assign_by_ref($camelBackedHelper,${$camelBackedHelper});
				//$this->Smarty->assign_by_ref($camelBackedHelper, ${$camelBackedHelper});
			}
		}

		//$this->register_functions();
		
		foreach($___data_for_view as $data => $value)
		{
			if(!is_object($data))
			{
				$this->Dzstyle->assign($data, $value);
			}
		}
		$this->Dzstyle->tplfile  =   $___viewFn;
		if($this->layout_flag)
		{
			return  $content = $this->Dzstyle->display($___viewFn);
		}
	}
	
	function renderLayout($content_for_layout, $layout = null) {
		$layoutFileName = $this->_getLayoutFileName($layout);
		if (empty($layoutFileName)) {
			return $this->output;
		}

		$debug = '';

		if (isset($this->viewVars['cakeDebug']) && Configure::read() > 2) {
			$params = array('controller' => $this->viewVars['cakeDebug']);
			$debug = View::element('dump', $params, false);
			unset($this->viewVars['cakeDebug']);
		}

		$dataForLayout = array_merge($this->viewVars, array(
			'content_for_layout' => $content_for_layout,
			'scripts_for_layout' => join("\n\t", $this->__scripts),
			'cakeDebug' => $debug
		));

		if (!isset($dataForLayout['title_for_layout'])) {
			$dataForLayout['title_for_layout'] = Inflector::humanize($this->viewPath);
		}

		if (empty($this->loaded) && !empty($this->helpers)) {
			$loadHelpers = true;
		} else {
			$loadHelpers = false;
			$dataForLayout = array_merge($dataForLayout, $this->loaded);
		}

		$this->_triggerHelpers('beforeLayout');
		$this->output = $this->_render($layoutFileName, $dataForLayout, $loadHelpers, true);

		if ($this->output === false) {
			$this->output = $this->_render($layoutFileName, $data_for_layout);
			$msg = __("Error in layout %s, got: <blockquote>%s</blockquote>", true);
			trigger_error(sprintf($msg, $layoutFileName, $this->output), E_USER_ERROR);
			return false;
		}

		$this->_triggerHelpers('afterLayout');

		return $this->output;
	}
	
/**
 * Returns layout filename for this template as a string.
 *
 * @return string Filename for layout file (.ctp).
 * @access private
 */
	function _getLayoutFileName() {
		//return null;
		$type = null;
		
		$this->layout_flag = true;

		if (isset($this->plugin) && !is_null($this->plugin)) {
			if (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext)) {
				$layoutFileName = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext;
				return $layoutFileName;
			}
		}
		$paths = $this->_paths(Inflector::underscore($this->plugin));
		
		foreach($paths as $path) {
			if (file_exists($path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext)) {
				$layoutFileName = $path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext;
				return $layoutFileName;
			}
		}

		if($layoutFileName = fileExistsInPath(LIBS . 'view' . DS . 'layouts' . DS . $type . $this->layout . '.ctp')) {
		} else {
			$layoutFileName = LAYOUTS . $type . $this->layout.$this->ext;
		}
		return $layoutFileName;
	}

/**
 * Returns filename of given action's template file (.tpl) as a string. CamelCased action names will be under_scored! This means that you can have LongActionNames that refer to long_action_names.ctp views.
 *  - added: will also return .ctp templates in viewPath paths for fallback
 *
 * @param string $action Controller action to find template filename for
 * @return string Template filename
 * @access private
 */
	function _getViewFileName($action) {
		$action = Inflector::underscore($action);

		$type = null;

		if (empty($action)) {
			$action = $this->action;
		}		
		$position = strpos($action, '..');

		if ($position === false) {
		} else {
			$action = explode('/', $action);
			$i = array_search('..', $action);
			unset($action[$i - 1]);
			unset($action[$i]);
			$action='..' . DS . implode(DS, $action);
		}
		
		$paths = $this->_paths(Inflector::underscore($this->plugin));
		
		$exts = array($this->ext);
		if ($this->ext !== '.ctp') {
			array_push($exts, '.ctp');
		}
		foreach ($exts as $ext) {			
			foreach($paths as $path) {	
				//echo '<br/>';	
				//echo $path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
				//echo '<br/>';	
				if (file_exists($path . $this->viewPath . DS . $this->subDir . $type . $action . $ext)) {
					$viewFileName = $path . $this->viewPath . DS . $this->subDir . $type . $action . $ext;
					return $viewFileName;
				}
			}
		}
		
		
		
		if ($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'errors' . DS . $type . $action . '.ctp')) {
		} elseif($viewFileName = fileExistsInPath(LIBS . 'view' . DS . $this->viewPath . DS . $type . $action . '.ctp')) {
		} else {
			$viewFileName = VIEWS . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
		}
		// 如果默认的操作模板，在主题的根目录下有模板，而在对应的model下没有模板，则调用主题下的模板
		if(!file_exists($viewFileName) && file_exists($this->Dzstyle->theme_path.'message.html'))
		{
			$viewFileName = $this->Dzstyle->theme_path.'message.html' ;
		}
		return $viewFileName;
	}
}
?>